/* 
 * Copyright IBM Japan, AMS Services 2003 ALL Rights Reserved.
 * 
 * This product includes software developed by 
 * the Apache Software Foundation (http://www.apache.org/).
 */

package com.tianqu.tms.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.commons.validator.GenericValidator;



/**
 * 
 * @author liugang
 *
 */
public class FieldChecker implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	//英文大写
	public final static String UPPER_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	//英文小写
    public final static String LOWER_ALPHABET = "abcdefghijklmnopqrstuvwxyz";
    //数字
    public final static String NUMBER = "1234567890";
    //特殊符合
    public final static String SYMBOL = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
    //空格
    public final static String SPACE = " ";
    //用户名的特殊符合
    public final static String SYMBOL_USERID = "@(-_.!#$%&'+/=?[]^`{|}~";
    //禁止文字的文件名称(properties)
	public static final String COMM_UNENABLE = "commUnenable";
	//车辆号牌的第一个汉字
	public static final String PROVINCE = "京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新港澳台空";//v305

	private FieldChecker () {
		super();
	}
	public static void main(String str[]) {
//        System.out.println(GenericValidator.matchRegexp("1.","^[1-9]{1}[0-9]*[.]{1}[0-9]*$"));
	}
	/**
	 * 验证字符串有效性
	 * @param value
	 * @param permitted
	 * @return
	 */
    public static boolean validateValidSt(String value,String permitted) {
    	if(StringUtil.isNullOrBlank(value) && StringUtil.isNullOrBlank(permitted)) {
			return true;
		}
        char[] charArray = value.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            if (permitted.indexOf((int) charArray[i]) == -1) {
                return false;
            }
        }
        return true;
    }
    /**
     * 省区号的check
     * @param _baseSt
     * @return
     */
    public static boolean isProvinceSt(String _baseSt) {
		//参数是null或者0长度的时候
		if(_baseSt == null || _baseSt.length() < 1) {
			return true;
		}
		//check开始
		return  validateValidSt(_baseSt,PROVINCE);
	}
	/**
	 * 英数字的check
	 *
	 * @param _baseSt   check的文字。
	 * @return boolean  符合要求：true,不符合要求：false
	 */
	public static boolean isAlphabetNumericalSt(String _baseSt) {
		//参数是null或者0长度的时候
		if(_baseSt == null || _baseSt.length() < 1) {
			return true;
		}
		//check开始
		return GenericValidator.matchRegexp(_baseSt, "^[0-9a-zA-Z]*$");
	}
	/**
	 * 英文字母的check
	 * @param _baseSt
	 * @return
	 */
	public static boolean isAlphabetSt(String _baseSt) {
		//参数是null或者0长度的时候
		if(_baseSt == null || _baseSt.length() < 1) {
			return false;
		}
		//check开始
		return GenericValidator.matchRegexp(_baseSt, "^[a-zA-Z]*$");
	}
	/**
	 * 数字的check
	 * @param _baseSt
	 * @return
	 */
	public static boolean isNumericalSt(String _baseSt) {
		//参数是null或者0长度的时候
		if(_baseSt == null || _baseSt.length() < 1) {
			return false;
		}
		//check开始
		return GenericValidator.matchRegexp(_baseSt, "^[0-9]*$");
	}
	/**
	 * 汉字的check
	 * @param _baseSt
	 * @return [\\u4e00-\\u9fa5]
	 */
	public static boolean isChineseSt(String _baseSt) {
		//参数是null或者0长度的时候
		if(_baseSt == null || _baseSt.length() < 1) {
			return false;
		}
		//check开始
		return GenericValidator.matchRegexp(_baseSt, "^[\\u4e00-\\u9fa5]*$");
	}
	/**
	 * 字符串的长度check
	 * @param szFieldValue
	 * @param iMinimumLength
	 * @param iMaxLength
	 * @return
	 */
	public static boolean checkLength(String szFieldValue, int iMinimumLength, int iMaxLength) {
		//ok
		if((szFieldValue.length() >= iMinimumLength) && (szFieldValue.length() <= iMaxLength)){
			return true;
		}
		else{
			return false;
		}
	}
	/**
	 * 指定格式日期check
	 * @param date
	 * @param format
	 * @return
	 */
	public static boolean isDate(String date, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		df.setLenient( false );
		try {
			df.parse(date);
		}
		catch (ParseException e) {
			return false;
		}
		return true;
	}
	public static boolean isNumber(String _baseSt) {
		//参数是null或者0长度的时候
		if(_baseSt == null || _baseSt.length() < 1) {
			return false;
		}
		//check开始
		return GenericValidator.matchRegexp(_baseSt, "^[1-9]{1}[0-9]*$");
	}
	
	/**
	 * 可以不填
	 * @param _baseSt
	 * @return
	 */
	public static boolean isNumber1(String _baseSt) {
		//参数是null或者0长度的时候
		if(_baseSt == null || _baseSt.length() < 1 || "0".equals(_baseSt)) {
			return true;
		}
		//check开始
		return GenericValidator.matchRegexp(_baseSt, "^[1-9]{1}[0-9]*$");
	}
}
